<template>
  <div>
    <div
      v-for="(item, index) in arr"
      :key="index"
      style="display: inline-black"
    >
      <input type="checkbox" :value="item" v-model="num" />
      <span>{{ item }}</span>
    </div>
    <p>选中的元素累加和为：{{ addFn }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [9, 15, 19, 25, 29, 31, 48, 57, 62, 79, 87],
      num: [],
    };
  },
  computed: {
    addFn() {
      return this.num.reduce((pre, val) => {
        return (pre += val);
      }, 0);
    },
  },
};
</script>

<style>
</style>

<div>
    <!-- 无id时, 可以使用index(反正也是就地更新) -->
    <div
      v-for="(item, index) in arr"
      style="display: inline-block"
      :key="index"
    >
      <input type="checkbox" v-model="checkNumArr" :value="item" />
      <span>{{ item }}</span>
    </div>
    <p>你选中的元素, 累加的值和为: {{ theSum }}</p>
  </div>



   data() {
    return {
      arr: [9, 15, 19, 25, 29, 31, 48, 57, 62, 79, 87],
      checkNumArr: [], //  保存用户选中的数字
    };
  },
  computed: {
    theSum() {
      return this.checkNumArr.reduce((sum, val) => {
        return (sum += val);
      }, 0);
    },
  },